<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;

class PasswordController extends BaseController
{
    /**
     * 修改密码
     */
    public function updatePassword(Request $request)
    {
        $request->validate([
                               'old_password' => 'required|min:6|max:16',
                               'password'     => 'required|min:6|max:16|confirmed',
                           ], [
                               'old_password.required' => '请输入旧密码',
                               'old_password.min'      => '旧密码至少6位',
                               'old_password.max'      => '旧密码最多16位',
                               'password.min'          => '新密码至少6位',
                               'password.max'          => '新密码最多16位',
                               'password.required'     => '请输入新密码',
                               'password.confirmed'    => '两次密码输入不一致',
                           ]);
        $old_password = $request->input('old_password');
        $user = auth('api')->user();
        //验证旧密码是否正确
        if (!password_verify($old_password, $user->password)){
            return $this->response->errorBadRequest('旧密码错误');
        }

        //更新用户密码
        $user->password = bcrypt($request->input('password'));
        $user->save();
        return $this->response->noContent();
    }

}
